package com.ipzoe.web.core.beetl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import org.beetl.core.Context;
import org.beetl.core.Function;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class GetUrlQuery implements Function {
    @Override
    public Object call(Object[] objects, Context context) {
        HttpServletRequest request = (HttpServletRequest)objects[0];
        String queryKey = String.valueOf(objects[1]);

        Map<String, String> paramMap = Maps.newHashMap();
        Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames.hasMoreElements()){
            String paramName = parameterNames.nextElement();
            paramMap.put(paramName, request.getParameter(paramName));
        }
        String value = paramMap.get(queryKey);
        if(StrUtil.isEmpty(value)){
            return "all";
        }
        return value;
    }

}
